<?php

/**
 * modelo_documento actions.
 *
 * @package    ibe
 * @subpackage modelo_documento
 * @author     Creatix
 */
class modelo_documentoActions extends sfActionsCreatix
{
	public function executeIndex(sfWebRequest $request)
	{
		return $this->forward('modelo_documento', 'list');
	}

	public function executeGrid()
	{
		//Recupero todas as informações necessárias para trabalhar com o grid
		$this->getRequestDataTable();
		$this->data					= ModeloDocumentoTable::consultar($this->filter, $this->order);
		$this->total_data			= count($this->data);
		$this->data					= $this->paginadorDataTable($this->data, $this->page_num, parent::LIMIT_PAGE_DATA_TABLE);

		sfLoader::loadHelpers(array('Url'));

		foreach($this->data as $key => &$r)
		{
			$r['id_tipo_documento'] = $r['TipoDocumento']['nm_tipo_documento'];
			$actions = array(
				'edit'	 => "<a href='".url_for($this->getModuleName().'/edit?id_modelo_documento='.$r['id_modelo_documento'])."' class='icoEditar edit'></a>",
				'delete' => "<a href='".url_for($this->getModuleName().'/delete?id_modelo_documento='.$r['id_modelo_documento'])."'  class='icoExcluir delete'></a>",
			);
			$this->data[$key]['act'] = implode(' ', $actions);
		}

		$this->setJSONDataTable($this->data, $this->colunas, $this->total_data);
		return sfView::NONE;
	}


	public function executeList()
	{
		$this->form = new ModeloDocumentoForm();
		$this->colunas	= array(
			$this->form['id_modelo_documento']->renderLabel() => "id_modelo_documento",
			$this->form['id_tipo_documento']->renderLabel() => "id_tipo_documento",
			$this->form['st_modelo_documento']->renderLabel() => "st_modelo_documento",
			"Ações"	=> "act"
		);

						$this->filter = $this->_getFilter();

						$this->setTemplate('index');
	}

	public function executeFilter()
	{
		$filter = new Filtro(
		$this->getRequestParameter(sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$filter->setCulture($this->getUser()->getCulture());
		$this->getUser()->setAttribute('pg_num',1,$this->getModuleName());

		$this->getUser()->setAttribute(
		sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE,
		serialize($filter),
		$this->getModuleName()
		);

		return $this->forward($this->getModuleName(), '/list');
	}

	public function executeShow(sfWebRequest $request)
	{
		$this->modelo_documento = Doctrine::getTable('ModeloDocumento')->find(array($request->getParameter('id_modelo_documento')));
		$this->forward404Unless($this->modelo_documento);
		$this->form = new ModeloDocumentoForm($this->modelo_documento);
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->form = new ModeloDocumentoForm();
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new ModeloDocumentoForm();

		$this->processForm($request, $this->form);

		$this->forward($this->getModuleName(), 'new');
	}

	public function validateCreate()
	{
		return true;
	}

	public function handleErrorCreate()
	{
		return $this->forward($this->getModuleName(), 'new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($modelo_documento = Doctrine::getTable('ModeloDocumento')->find(array($request->getParameter('id_modelo_documento'))), sprintf('Object modelo_documento does not exist (%s).', $request->getParameter('id_modelo_documento')));
		$this->form = new ModeloDocumentoForm($modelo_documento);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($modelo_documento = Doctrine::getTable('ModeloDocumento')->find(array($request->getParameter('id_modelo_documento'))), sprintf('Object modelo_documento does not exist (%s).', $request->getParameter('id_modelo_documento')));
		$this->form = new ModeloDocumentoForm($modelo_documento);

		$this->processForm($request, $this->form);

		$this->forward($this->getModuleName(), 'edit');
	}

	public function validateUpdate()
	{
		return true;
	}

	public function handleErrorUpdate()
	{
		return $this->forward($this->getModuleName(), 'edit');
	}

	public function executeDelete(sfWebRequest $request)
	{
		try
		{
			$request->checkCSRFProtection();

			$this->forward404Unless($modelo_documento = Doctrine::getTable('ModeloDocumento')->find(array($request->getParameter('id_modelo_documento'))), sprintf('Object modelo_documento does not exist (%s).', $request->getParameter('id_modelo_documento')));
			$modelo_documento->delete();

			$this->redirect('modelo_documento/index');
		}
		catch(Excepiton $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
			$this->redirect('modelo_documento/index');
		}

	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		try
		{
			$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
			if ($form->isValid())
			{
				$modelo_documento = $form->save();
				$this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
				$this->redirect('modelo_documento/edit?id_modelo_documento='.$modelo_documento->getIdModeloDocumento());
			}
			else
			{
				throw new Exception($form->getErrorSchema());
			}
		}
		catch(Exception $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
		}
	}
}
